package com.jsb.controller;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jsb.service.UserService;
import com.jsb.service.WishService;
import com.jsb.service.WishbuyService;
import com.jsb.service.WishcodeService;
import com.jsb.common.config.Const;
import com.jsb.common.config.Global;
import com.jsb.common.utils.BeanUtils;
import com.jsb.common.utils.ShortUuid;
import com.jsb.common.utils.StringUtils;
import com.jsb.dto.WishDto;
import com.jsb.model.User;
import com.jsb.model.Wish;
import com.jsb.model.WishExample;
import com.jsb.model.Wishbuy;
import com.jsb.model.Wishcode;
import com.jsb.model.WishcodeExample;
import com.jsb.model.WishcodeExample.Criteria;

@Controller
public class WishController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/wish/list.do";
	
	@Resource
	private WishService wishService;
	
	@Resource
	private WishbuyService wishbuyService;
	
	@Resource
	private WishcodeService wishcodeService;
	
	@Resource
	private UserService userService;
	
	@RequestMapping("/wish/list")
	public String list(
			Model model,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		WishExample wishExample = new WishExample();
		int totalCount = wishService.countWish(wishExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		wishExample.setPageIndex((pageNum - 1) * pageSize);
		wishExample.setPageSize(pageSize);
		List<Wish> wishes = wishService.getPageWish(wishExample);
		List<WishDto> wishs = new ArrayList<WishDto>();
		for (Wish wish : wishes) {
			WishDto dto = new WishDto();
			BeanUtils.copyProperties(dto, wish);
			String id = wish.getId();
			Wishbuy wishbuy = new Wishbuy();
			wishbuy.setWishId(id);
			wishbuy.setStatus(Global.PAYSUC);
			double sum = wishbuyService.sum(wishbuy);
			double progress = sum/wish.getTotalPrice();
			dto.setProgress(StringUtils.formatDouble(progress));
			dto.setCurrenCount((int) sum);
			wishs.add(dto);
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("wishs", wishs);
		return "/Wish/list";
	}
	/**
	 * 最后50名许愿毫秒数之和
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/wish/sumtime")
	public String sumtime(Model model,
			@RequestParam(value = "id", required = true) String id){
		Wish wishById = wishService.getWishById(id);
		Integer totalPrice = wishById.getTotalPrice();
		Wishbuy wishbuy = new Wishbuy();
		wishbuy.setWishId(id);
		wishbuy.setStatus(Global.PAYSUC);
		double sum = wishbuyService.sum(wishbuy);
		if(sum<totalPrice){
			if(Const.isDebug()){
				long sumLastTime = wishbuyService.sumLastTime(wishbuy);
				model.addAttribute("sumLastTime", Long.toString(sumLastTime));
			}
		}else{
			long sumLastTime = wishbuyService.sumLastTime(wishbuy);
			model.addAttribute("sumLastTime", Long.toString(sumLastTime));
		}
		model.addAttribute("wish", wishById);
		return "/Wish/lottery";
	}
	@RequestMapping("/wish/lottery")
	public String lottery(Model model,@ModelAttribute("wish") Wish wish,String sumLastTime,String lotteryResult){
		String id = wish.getId();
		Wish wishById = wishService.getWishById(id);
		Integer totalPrice = wishById.getTotalPrice();
		long n = Long.valueOf(sumLastTime) + Long.valueOf(lotteryResult);
		long l = n % totalPrice;
		Integer startNum = wishById.getStartNum() + 1;
		long wishcode = l + startNum;
		WishcodeExample example = new WishcodeExample();
		Criteria criteria = example.createCriteria();
		criteria.andCodeEqualTo(String.valueOf(wishcode));
		criteria.andWishIdEqualTo(id);
		List<Wishcode> list = wishcodeService.selectByExample(example);
		if(null != list && list.size()==1){
			String userid = list.get(0).getUserId();
			User user = userService.getUserByPhoneNumber(userid);
			model.addAttribute("user", user);
		}else{
			
		}
		model.addAttribute("wishcode", wishcode);
		model.addAttribute("wish", wishById);
		model.addAttribute("sumLastTime", sumLastTime);
		model.addAttribute("lotteryResult", lotteryResult);
		return "/Wish/lotteryResult";
	}
	
	@RequestMapping("/wish/toAdd")
	public String toAdd(Model model) {
		return "/Wish/add";
	}

	@RequestMapping("/wish/add")
	public String add(Model model, @ModelAttribute("wish") Wish wish) {
		wish.setId(ShortUuid.generateShortUuid());
		wishService.addWish(wish);
		return redirect;
	}

	@RequestMapping("/wish/del")
	public String del(Model model,
			@RequestParam(value = "id", required = true) String id) {
		wishService.delWishById(id);
		return redirect;
	}

	@RequestMapping("/wish/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "id", required = true) String id) {
		Wish wish = wishService.getWishById(id);
		model.addAttribute("wish", wish);
		return "/Wish/edit";
	}

	@RequestMapping("/wish/edit")
	public String edit(Model model, @ModelAttribute("wish") Wish wish) {
		wishService.updateWish(wish);
		return redirect;
	}

	@RequestMapping("/wish/view")
	public String view(Model model,
			@RequestParam(value = "id", required = true) String id) {
		Wish wish = wishService.getWishById(id);
		model.addAttribute("wish", wish);
		return "/Wish/view";
	}
}
